{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "       Month        Category  Amount\n0    January  Transportation    74.0\n1    January         Grocery   235.0\n2    January       Household   175.0\n3    January   Entertainment   100.0\n4   February  Transportation   115.0\n5   February         Grocery   240.0\n6   February       Household   225.0\n7   February   Entertainment   125.0\n8      March  Transportation    90.0\n9      March         Grocery   260.0\n10     March       Household   200.0\n11     March   Entertainment   120.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Month</th>\n      <th>Category</th>\n      <th>Amount</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>January</td>\n      <td>Transportation</td>\n      <td>74.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>January</td>\n      <td>Grocery</td>\n      <td>235.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>January</td>\n      <td>Household</td>\n      <td>175.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>January</td>\n      <td>Entertainment</td>\n      <td>100.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>February</td>\n      <td>Transportation</td>\n      <td>115.0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>February</td>\n      <td>Grocery</td>\n      <td>240.0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>February</td>\n      <td>Household</td>\n      <td>225.0</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>February</td>\n      <td>Entertainment</td>\n      <td>125.0</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>March</td>\n      <td>Transportation</td>\n      <td>90.0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>March</td>\n      <td>Grocery</td>\n      <td>260.0</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>March</td>\n      <td>Household</td>\n      <td>200.0</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>March</td>\n      <td>Entertainment</td>\n      <td>120.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example = pd.DataFrame({'Month': [\"January\", \"January\", \"January\", \"January\", \"February\", \"February\", \"February\",\n",
    "                                  \"February\", \"March\", \"March\", \"March\", \"March\"],\n",
    "                        'Category': [\"Transportation\", \"Grocery\", \"Household\", \"Entertainment\", \"Transportation\",\n",
    "                                     \"Grocery\", \"Household\", \"Entertainment\", \"Transportation\", \"Grocery\", \"Household\",\n",
    "                                     \"Entertainment\"],\n",
    "                        'Amount': [74., 235., 175., 100., 115., 240., 225., 125., 90., 260., 200., 120.]})\n",
    "example"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "Month           February  January  March\nCategory                                \nEntertainment      125.0    100.0  120.0\nGrocery            240.0    235.0  260.0\nHousehold          225.0    175.0  200.0\nTransportation     115.0     74.0   90.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Month</th>\n      <th>February</th>\n      <th>January</th>\n      <th>March</th>\n    </tr>\n    <tr>\n      <th>Category</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Entertainment</th>\n      <td>125.0</td>\n      <td>100.0</td>\n      <td>120.0</td>\n    </tr>\n    <tr>\n      <th>Grocery</th>\n      <td>240.0</td>\n      <td>235.0</td>\n      <td>260.0</td>\n    </tr>\n    <tr>\n      <th>Household</th>\n      <td>225.0</td>\n      <td>175.0</td>\n      <td>200.0</td>\n    </tr>\n    <tr>\n      <th>Transportation</th>\n      <td>115.0</td>\n      <td>74.0</td>\n      <td>90.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example_pivot = example.pivot(index='Category', columns='Month', values='Amount')\n",
    "example_pivot"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "Category\nEntertainment     345.0\nGrocery           735.0\nHousehold         600.0\nTransportation    279.0\ndtype: float64"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example_pivot.sum(axis=1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "Month\nFebruary    705.0\nJanuary     584.0\nMarch       670.0\ndtype: float64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "example_pivot.sum(axis=0)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "Pclass           1          2          3\nSex                                     \nfemale  106.125798  21.970121  16.118810\nmale     67.226127  19.741782  12.661633",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Pclass</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n    </tr>\n    <tr>\n      <th>Sex</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>female</th>\n      <td>106.125798</td>\n      <td>21.970121</td>\n      <td>16.118810</td>\n    </tr>\n    <tr>\n      <th>male</th>\n      <td>67.226127</td>\n      <td>19.741782</td>\n      <td>12.661633</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('./data/titanic.csv')\n",
    "df.pivot_table(index='Sex', columns='Pclass', values='Fare')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "Pclass         1     2      3\nSex                          \nfemale  512.3292  65.0  69.55\nmale    512.3292  73.5  69.55",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Pclass</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n    </tr>\n    <tr>\n      <th>Sex</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>female</th>\n      <td>512.3292</td>\n      <td>65.0</td>\n      <td>69.55</td>\n    </tr>\n    <tr>\n      <th>male</th>\n      <td>512.3292</td>\n      <td>73.5</td>\n      <td>69.55</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index='Sex', columns='Pclass', values='Fare', aggfunc='max')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "Pclass    1    2    3\nSex                  \nfemale   94   76  144\nmale    122  108  347",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Pclass</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n    </tr>\n    <tr>\n      <th>Sex</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>female</th>\n      <td>94</td>\n      <td>76</td>\n      <td>144</td>\n    </tr>\n    <tr>\n      <th>male</th>\n      <td>122</td>\n      <td>108</td>\n      <td>347</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index='Sex', columns='Pclass', values='Fare', aggfunc='count')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "Sex          female      male\nUnderaged                    \nFalse      0.760163  0.167984\nTrue       0.676471  0.338028",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>Sex</th>\n      <th>female</th>\n      <th>male</th>\n    </tr>\n    <tr>\n      <th>Underaged</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>False</th>\n      <td>0.760163</td>\n      <td>0.167984</td>\n    </tr>\n    <tr>\n      <th>True</th>\n      <td>0.676471</td>\n      <td>0.338028</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['Underaged'] = df['Age'] <= 18\n",
    "df.pivot_table(index='Underaged', columns='Sex', values='Survived', aggfunc='mean')"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
